package com.a01_2021.thousandstimes.general.service.impl;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.exception.CustomExceptionType;
import com.a01_2021.thousandstimes.general.entity.SysMenu;
import com.a01_2021.thousandstimes.general.entity.SysRoleMenu;
import com.a01_2021.thousandstimes.general.mapper.SysMenuMapper;
import com.a01_2021.thousandstimes.general.mapper.SysRoleMenuMapper;
import com.a01_2021.thousandstimes.general.service.SysMenuService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author FRQ
 * @since 2021-02-22
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Resource
    SysMenuMapper sysMenuMapper;

    @Resource
    SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 检查列表内是否均为有效权限
     *
     * @param setMenuList 查询的权限List
     * @return 返还是否均为有效权限
     */
    @Override
    public boolean isMenu(Set<Long> setMenuList) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        for (long item : setMenuList) {
            queryWrapper.or().eq("id", item);
        }
        List<SysMenu> sysMenuList = sysMenuMapper.selectList(queryWrapper);
        return setMenuList.size() == sysMenuList.size();
    }

    /**
     * 检查是否均为有效权限
     *
     * @param menuName 查询的权限名
     * @return 返还是否为有效权限
     */
    public boolean isMenu(String menuName) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(true, "name", menuName);
        SysMenu sysMenu = sysMenuMapper.selectOne(queryWrapper);
        return sysMenu != null;
    }

    /**
     * 检查是否均为有效权限
     *
     * @param menuId 查询的权限名
     * @return 返还是否为有效权限
     */
    public boolean isMenu(long menuId) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(true, "id", menuId);
        SysMenu sysMenu = sysMenuMapper.selectOne(queryWrapper);
        return sysMenu != null;
    }

    /**
     * 用于添加权限
     *
     * @param menuName 权限名
     * @param url      访问url
     * @return 返还添加后权限SysMenu
     * @throws CustomException 当权限已存在时抛出该异常
     */
    public SysMenu addMenu(String menuName, String url) throws CustomException {
        if (this.isMenu(menuName)) {
            //若权限已存在
            throw new CustomException(CustomExceptionType.MENU_HAD_BEEN_SET_ERROR, "权限已存在");
        }
        SysMenu sysMenu = new SysMenu();
        sysMenu.setName(menuName);
        sysMenu.setUrl(url);
        sysMenuMapper.insert(sysMenu);
        return sysMenu;
    }

    /**
     * 通过权限id检查权限是否未闲置（分配到角色
     *
     * @param menuId 权限id
     * @return 是否未闲置
     */
    public Boolean notMenuIdle(long menuId) {
        QueryWrapper<SysRoleMenu> sysRoleMenuQueryWrapper = new QueryWrapper<>();
        sysRoleMenuQueryWrapper.eq("menu_id", menuId);
        int count = sysRoleMenuMapper.selectCount(sysRoleMenuQueryWrapper);
        return count != 0;
    }

    /**
     * 通过权限id删除权限
     *
     * @param menuId 权限id
     * @throws CustomException 当权限不存在，权限未闲置时抛出该异常
     */
    @Override
    public void removeMenu(long menuId) throws CustomException {
        if (!this.isMenu(menuId)) {
            //权限不存在
            throw new CustomException(CustomExceptionType.MENU_NOT_FOUNT_ERROR, "删除权限不存在");
        }
        if (this.notMenuIdle(menuId)) {
            //权限未闲置
            throw new CustomException(CustomExceptionType.MENU_NOT_IDLE, "权限未闲置（仍分配到角色");
        }
        sysRoleMenuMapper.deleteById(menuId);
    }

    /**
     * 从主数据获取全部权限
     *
     * @return List 权限列表
     */
    @DS("master")
    public List<SysMenu> getMenuFromMaster() {
        return sysMenuMapper.selectList(null);
    }

}
